using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class SessionReputationManager : MonoBehaviour
{
	private static SessionReputationManager instance;

	public List<SessionReputationReward> savedListReward = new List<SessionReputationReward>();

	public string keySaveReward = "keySaveReward";

	private SaveFloat reputationCount;

	private SaveInt startRewardIndex;

	private SaveInt currentRewardIndex;

	[SerializeField]
	private List<SessionReputationProbalityReward> rewardList;

	[SerializeField]
	private List<SessionReputationReward> generatedRewardList;

	private bool allRewardRecieved;

	public static string keyForOldPlayers = "keyForOldPlayers";

	[SerializeField]
	private List<LeagueReward> rewardListLeague = new List<LeagueReward>();

	public static SessionReputationManager Instance
	{
		get
		{
			if (instance == null)
			{
				UnityEngine.Object.Instantiate(Resources.Load<GameObject>("SessionReputationManager")).name = "SessionReputationManager(Don't Destroy)";
			}
			return instance;
		}
	}

	public int ReceivedRewardCount
	{
		get
		{
			return (int)currentRewardIndex - (int)startRewardIndex;
		}
	}

	public List<SessionReputationProbalityReward> RewardList
	{
		get
		{
			return rewardList;
		}
	}

	public int SessionReputationChanged
	{
		get
		{
			return (int)reputationCount.GetValue();
		}
	}

	public event Action<float, float> reputationChanged;

	public event Action<bool> rewardChanged;

	private void Awake()
	{
		instance = this;
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		string text = BCWStorage.Get<string>(keySaveReward);
		string text2 = string.Empty;
		string text3 = text;
		for (int i = 0; i < text3.Length; i++)
		{
			char c = text3[i];
			if (c != ' ')
			{
				text2 += c;
				continue;
			}
			BCWDebug.LogError(text2);
			int result = 0;
			int.TryParse(text2, out result);
			savedListReward.Add((SessionReputationReward)rewardList[result]);
			text2 = string.Empty;
		}
		for (int j = 0; j < rewardList.Count; j++)
		{
			SessionReputationProbalityReward sessionReputationProbalityReward = default(SessionReputationProbalityReward);
			sessionReputationProbalityReward = rewardList[j];
			sessionReputationProbalityReward.index = j;
			rewardList[j] = sessionReputationProbalityReward;
		}
	}

	public void SaveRewardOnQuit()
	{
		List<SessionReputationReward> list = new List<SessionReputationReward>();
		list = GetRewardList();
		if (list.Count != 0)
		{
			string text = string.Empty;
			BCWDebug.LogError("Save");
			for (int i = 0; i < list.Count; i++)
			{
				text = text + list[i].index + " ";
			}
			BCWStorage.Set(keySaveReward, text, true);
		}
	}

	public bool IsReputationTypeGame()
	{
		TypeGame curTypeGame = GameController.instance.curTypeGame;
		if (curTypeGame == TypeGame.race || curTypeGame == TypeGame.tanks || curTypeGame == TypeGame.TDM || curTypeGame == TypeGame.Zombe || curTypeGame == TypeGame.SkyBlock || curTypeGame == TypeGame.CopsAndRobbers || curTypeGame == TypeGame.Pixelmon || curTypeGame == TypeGame.WalkingRobots || curTypeGame == TypeGame.TitanfallV2)
		{
			return false;
		}
		return true;
	}

	public float AddReputation(float value)
	{
		if (!IsReputationTypeGame())
		{
			return 0f;
		}
		if (allRewardRecieved)
		{
			return reputationCount.GetValue();
		}
		reputationCount = reputationCount.GetValue() + value;
		if (reputationCount.GetValue() < (float)generatedRewardList[startRewardIndex].reputationValue)
		{
			reputationCount = generatedRewardList[startRewardIndex].reputationValue;
		}
		if ((int)currentRewardIndex < generatedRewardList.Count - 1 && reputationCount.GetValue() > (float)generatedRewardList[(int)currentRewardIndex + 1].reputationValue)
		{
			currentRewardIndex = (int)currentRewardIndex + 1;
			if (this.rewardChanged != null)
			{
				this.rewardChanged(true);
			}
			if ((int)currentRewardIndex == generatedRewardList.Count - 1)
			{
				allRewardRecieved = true;
			}
		}
		if (currentRewardIndex.GetValue() > startRewardIndex.GetValue() && reputationCount.GetValue() < (float)generatedRewardList[currentRewardIndex].reputationValue)
		{
			currentRewardIndex = (int)currentRewardIndex - 1;
			if (this.rewardChanged != null)
			{
				this.rewardChanged(false);
			}
		}
		if (this.reputationChanged != null && value != 0f)
		{
			float value2 = 1f;
			if ((int)currentRewardIndex < generatedRewardList.Count - 1)
			{
				value2 = (reputationCount.GetValue() - (float)generatedRewardList[currentRewardIndex].reputationValue) / (float)(generatedRewardList[(int)currentRewardIndex + 1].reputationValue - generatedRewardList[currentRewardIndex].reputationValue);
			}
			this.reputationChanged(Mathf.Clamp01(value2), value);
		}
		BCWStorage.Set("LastReputationRewardIndex", (int)currentRewardIndex, true);
		return reputationCount.GetValue();
	}

	public void GiveReward(Vector3 position, List<SessionReputationReward> listReward, bool withAnimation = true)
	{
		for (int i = 0; i < listReward.Count; i++)
		{
			if (listReward[i].type.Equals(SessionReputationRewardType.Cash))
			{
				if (withAnimation)
				{
					settings.ShowSpecialLabelWithAnimation(listReward[i].rewardCount, position, false);
				}
				else
				{
					settings.IncreaseExeptions(listReward[i].rewardCount);
				}
			}
			else if (listReward[i].type.Equals(SessionReputationRewardType.Gold))
			{
				if (withAnimation)
				{
					settings.ShowLabelWithAnimation(listReward[i].rewardCount, position, false);
				}
				else
				{
					settings.IncreaseWarnings(listReward[i].rewardCount);
				}
			}
			else if (listReward[i].type.Equals(SessionReputationRewardType.Star))
			{
				if (GameController.instance != null)
				{
					if (settings.offlineMode)
					{
						LeaderBoardServer.instance.SendStars(listReward[i].rewardCount, TypeGame.Offline);
					}
					else
					{
						LeaderBoardServer.instance.SendStars(listReward[i].rewardCount, GameController.instance.curTypeGame);
					}
				}
				CupsManager.Cups += listReward[i].rewardCount;
			}
			else
			{
				GiveProductReward(listReward[i]);
			}
		}
		reputationCount = 0f;
		if (listReward.Count > 0)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Clear();
			dictionary.Add("Count reward", listReward.Count.ToString());
			FlurryWrapper.LogEvent(FlurryWrapper.EV_COUNT_SESSION_REPUTATION_RECIEVED_REWARD, dictionary);
		}
		BCWStorage.Set(keySaveReward, string.Empty, true);
	}

	public List<SessionReputationReward> GetRewardList()
	{
		List<SessionReputationReward> list = new List<SessionReputationReward>();
		for (int i = (int)startRewardIndex + 1; i <= (int)currentRewardIndex; i++)
		{
			list.Add(generatedRewardList[i]);
		}
		return list;
	}

	private void GiveProductReward(SessionReputationReward reward)
	{
		Product product = MarketController.Instance.GetProduct(reward.rewardID);
		if (product.Purchased)
		{
			if (product.PriceIn == MoneyType.Cash)
			{
				settings.CathedExeptions += (int)((float)product.Price * 0.5f);
			}
			else
			{
				settings.CathedWarnings += (int)((float)product.Price * 0.5f);
			}
		}
		else
		{
			MarketController.Instance.BuyProduct(product, true, reward.rewardCount);
		}
	}

	public void GetReceivedReward(ref SessionReputationReward receivedReward, ref SessionReputationReward nextReward, ref SessionReputationReward nextNextReward, ref int receivedRewardCount, ref int nextRewardCount)
	{
		if ((int)currentRewardIndex < generatedRewardList.Count - 1)
		{
			nextReward = generatedRewardList[(int)currentRewardIndex + 1];
		}
		else
		{
			nextReward = null;
		}
		if ((int)currentRewardIndex < generatedRewardList.Count - 2)
		{
			nextNextReward = generatedRewardList[(int)currentRewardIndex + 2];
		}
		else
		{
			nextNextReward = null;
		}
		if ((int)currentRewardIndex > (int)startRewardIndex)
		{
			receivedReward = generatedRewardList[currentRewardIndex];
		}
		else
		{
			receivedReward = null;
		}
		receivedRewardCount = (int)currentRewardIndex - (int)startRewardIndex;
		nextRewardCount = generatedRewardList.Count - 1 - (int)currentRewardIndex;
	}

	public void StartGame(League startGameLeague)
	{
		int num = BCWStorage.Get<int>("LastReputationRewardIndex");
		if (num < 0 || num >= 20)
		{
			num = 0;
		}
		startRewardIndex = num;
		currentRewardIndex = startRewardIndex;
		allRewardRecieved = false;
		reputationCount = 0f;
		GenerateReward(startGameLeague);
	}

	public void SetRewardList(List<SessionReputationProbalityReward> list)
	{
		rewardList.Clear();
		rewardList.AddRange(list);
	}

	private void GenerateReward(League league)
	{
		if (generatedRewardList != null)
		{
			generatedRewardList.Clear();
		}
		generatedRewardList = new List<SessionReputationReward>();
		generatedRewardList.Add(new SessionReputationReward());
		generatedRewardList[0].reputationValue = 0;
		List<SessionReputationProbalityReward> source = rewardList.Where((SessionReputationProbalityReward x) => x.league == league).ToList();
		int i = 1;
		List<SessionReputationProbalityReward> list;
		while ((list = source.Where((SessionReputationProbalityReward x) => x.rewardNumber == i).ToList()).Count > 0)
		{
			float max = list.Sum((SessionReputationProbalityReward x) => x.weight);
			float num = UnityEngine.Random.Range(0f, max);
			float num2 = 0f;
			int num3 = 0;
			while ((num2 += list[num3].weight) < num)
			{
				num3++;
			}
			SessionReputationReward sessionReputationReward = new SessionReputationReward();
			generatedRewardList.Add((SessionReputationReward)list[num3]);
			generatedRewardList[i].reputationValue += generatedRewardList[i - 1].reputationValue;
			i++;
		}
	}

	public void DontGetReward()
	{
		for (int i = 0; i <= CupsManager.CurrentLeagueIndex; i++)
		{
			BCWStorage.Set("RewardLeague" + rewardListLeague[i].league, true);
		}
	}

	public List<SessionReputationReward> GetLeagueReward()
	{
		if (!BCWStorage.Get<bool>(keyForOldPlayers))
		{
			for (int i = 0; i <= CupsManager.CurrentLeagueIndex; i++)
			{
				BCWStorage.Set("RewardLeague" + rewardListLeague[i].league, true);
				BCWStorage.Set(keyForOldPlayers, true);
			}
		}
		List<SessionReputationReward> list = new List<SessionReputationReward>();
		for (int j = 0; j <= CupsManager.CurrentLeagueIndex; j++)
		{
			if (BCWStorage.Get<bool>("RewardLeague" + rewardListLeague[j].league))
			{
				continue;
			}
			BCWStorage.Set("RewardLeague" + rewardListLeague[j].league, true);
			if (rewardListLeague[j].cashCount > 0)
			{
				SessionReputationReward sessionReputationReward = new SessionReputationReward();
				sessionReputationReward.type = SessionReputationRewardType.Cash;
				sessionReputationReward.upLeague = true;
				sessionReputationReward.rewardCount = rewardListLeague[j].cashCount;
				list.Add(sessionReputationReward);
			}
			if (rewardListLeague[j].goldCount > 0)
			{
				SessionReputationReward sessionReputationReward2 = new SessionReputationReward();
				sessionReputationReward2.type = SessionReputationRewardType.Gold;
				sessionReputationReward2.upLeague = true;
				sessionReputationReward2.rewardCount = rewardListLeague[j].goldCount;
				list.Add(sessionReputationReward2);
			}
			foreach (string item in rewardListLeague[j].idGotcha)
			{
				if (item != string.Empty)
				{
					SessionReputationReward sessionReputationReward3 = new SessionReputationReward();
					sessionReputationReward3.type = SessionReputationRewardType.Product;
					sessionReputationReward3.upLeague = true;
					sessionReputationReward3.rewardCount = 1;
					sessionReputationReward3.rewardID = item;
					list.Add(sessionReputationReward3);
				}
			}
		}
		return list;
	}

	public void AddReputationForReward(int indexReward)
	{
		if ((float)generatedRewardList[indexReward].reputationValue > reputationCount.GetValue())
		{
			AddReputation((float)generatedRewardList[indexReward].reputationValue - reputationCount.GetValue() + 10f);
		}
	}
}
